"use strict";
cc._RF.push(module, '747a7eU3jJOxaa0WHlKiFHH', 'soldier_node');
// scripts/soldier_node.js

"use strict";

var e = require;
var t = module;
var o = exports;

var _ii,
    nn = void 0 && (void 0).__extends || (_ii = function ii(t, e) {
  return (_ii = Object.setPrototypeOf || {
    __proto__: []
  } instanceof Array && function (t, e) {
    t.__proto__ = e;
  } || function (t, e) {
    for (var o in e) {
      Object.prototype.hasOwnProperty.call(e, o) && (t[o] = e[o]);
    }
  })(t, e);
}, function (t, e) {
  function o() {
    this.constructor = t;
  }

  _ii(t, e), t.prototype = null === e ? Object.create(e) : (o.prototype = e.prototype, new o());
}),
    rr = void 0 && (void 0).__awaiter || function (t, e, o, i) {
  return new (o || (o = Promise))(function (n, a) {
    function r(t) {
      try {
        l(i.next(t));
      } catch (e) {
        a(e);
      }
    }

    function s(t) {
      try {
        l(i["throw"](t));
      } catch (e) {
        a(e);
      }
    }

    function l(t) {
      var e;
      t.done ? n(t.value) : (e = t.value, e instanceof o ? e : new o(function (t) {
        t(e);
      })).then(r, s);
    }

    l((i = i.apply(t, e || [])).next());
  });
};

var oo = void 0 && (void 0).__decorate || function (e, t, a, n) {
  var i,
      o = arguments.length,
      s = o < 3 ? t : null === n ? n = Object.getOwnPropertyDescriptor(t, a) : n;
  if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) s = Reflect.decorate(e, t, a, n);else for (var r = e.length - 1; r >= 0; r--) {
    (i = e[r]) && (s = (o < 3 ? i(s) : o > 3 ? i(t, a, s) : i(t, a)) || s);
  }
  return o > 3 && s && Object.defineProperty(t, a, s), s;
};

Object.defineProperty(o, "__esModule", {
  value: !0
});

var a = e("BaseModuleUI"),
    n = e("GlobalParams"),
    i = e("CommonHelper"),
    r = e("PureHelper"),
    s = e("Game"),
    l = e("ObjectPool"),
    _ = e("node_team"),
    c = e("role_effects_info"),
    d = e("role_node"),
    h = e("team_node_info"),
    u = e("tower_node_info"),
    p = cc._decorator,
    m = p.ccclass,
    f = p.property,
    v = function (e) {
  function t() {
    var t = null !== e && e.apply(this, arguments) || this;
    return t.soldier_skin = null, t.team_info = null, t.target_node = null, t.soldier_kill_id = 0, t.soldier_rigidbody = null, t.next_index = 0, t.road_index = 0, t.target_list = [], t;
  }

  return nn(t, e), t.prototype.initView = function () {
    this.team_info = this.node.getComponent(h["default"]), this.node.scaleX = this.team_info.team_info == s.TeamInfo.left_team ? -1 : 1, this.soldier_kill_id = this.team_info.soldier_kill_id, this.soldier_rigidbody = this.node.getComponent(cc.RigidBody), this.soldierMove();
  }, t.prototype.showSoldierPos = function (e, t) {
    this.road_index = e, this.target_list = [], this.next_index = t == s.TeamInfo.right_team ? 0 : 6, this.node.setPosition(s["default"].game_path_pos[this.road_index][this.next_index]), t == s.TeamInfo.right_team ? this.next_index++ : this.next_index--;
  }, t.prototype.soldierMove = function () {
    var e = this;

    if (n.GlobalParams.GameStart && this.node.isValid) {
      var t = cc.v2(0, 0),
          o = 9999;
      if (this.target_list.length < 1) this.target_node = null, t = s["default"].game_path_pos[this.road_index][this.next_index];else if (null != this.target_node && -1 != this.target_list.indexOf(this.target_node)) o = Math.abs(r["default"].Distance(this.node.getPosition(), this.target_node.getPosition()));else {
        this.target_node = null;

        for (var a = 0; a < this.target_list.length; a++) {
          var i = this.target_list[a];

          if (i.active) {
            var l = Math.abs(r["default"].Distance(this.node.getPosition(), i.getPosition()));
            o > l && (o = l, this.target_node = i);
          }
        }
      }

      var _ = new cc.Vec2(0, 0);

      _ = null == this.target_node ? cc.v2(t.x, t.y).sub(cc.v2(this.node.x, this.node.y)).normalize() : cc.v2(this.target_node.x, this.target_node.y).sub(cc.v2(this.node.x, this.node.y)).normalize();
      var c = this.node.getPosition().add(_.mul(10));
      this.node.scaleX = this.node.x > c.x ? 1 : -1, o < this.team_info.node_info.ma_range ? Math.abs(this.target_node.y - this.node.y) > 80 ? (c = this.node.getPosition().add(cc.v2(0, this.target_node.y > this.node.y ? 1 : -1).mul(10)), "run" != this.soldier_skin.animation && this.soldier_skin.setAnimation(0, "run", !0), cc.tween(this.node).to(n.GlobalParams.GameUpdateTime, {
        position: cc.v2(c.x, c.y)
      }).call(function () {
        e.soldier_rigidbody.syncPosition(!0), e.soldierMove();
      }).start()) : this.attackFoeSoldier() : ("run" != this.soldier_skin.animation && this.soldier_skin.setAnimation(0, "run", !0), cc.tween(this.node).to(n.GlobalParams.GameUpdateTime, {
        position: cc.v2(c.x, c.y)
      }).call(function () {
        e.soldier_rigidbody.syncPosition(!0), Math.abs(r["default"].Distance(e.node.getPosition(), t)) < 100 && (e.team_info.team_info == s.TeamInfo.right_team ? e.next_index++ : e.next_index--, e.next_index = e.next_index > 6 ? 6 : e.next_index, e.next_index = e.next_index < 0 ? 0 : e.next_index), e.soldierMove();
      }).start());
    }
  }, t.prototype.attackFoeSoldier = function () {
    var e = this;
    this.soldier_skin.setAnimation(0, "attack", !1), this.soldier_skin.setCompleteListener(function () {
      e.soldier_skin.setCompleteListener(null), e.target_node.isValid && ("soldier_team_" == e.target_node.name.substring(0, 13) ? e.target_node.getComponent(h["default"]).soldierBeHit(e.team_info.node_info.ma_attack, e.soldier_kill_id) : "role_team" == e.target_node.name.substring(0, 9) ? e.target_node.getComponent(d["default"]).roleBeHit(e.team_info.node_info.ma_attack, e.soldier_kill_id) : "tower" == e.target_node.name.substring(0, 5) && e.target_node.getComponent(u["default"]).towerBeHit(e.team_info.node_info.ma_attack, e.soldier_kill_id)), e.scheduleOnce(function () {
        e.soldierMove();
      }, n.GlobalParams.GameUpdateTime);
    });
  }, t.prototype.onBeginContact = function (e, t, o) {
    var a = o.node.group;
    "role" != a && "soldier" != a || (e.disabled = !0);
  }, t.prototype.onCollisionEnter = function (e, t) {
    var o = e.node;
    o.group, o.isValid && 1 == t.tag && 0 == e.tag && this.getSoliderInfo(o) && this.target_list.push(o);
  }, t.prototype.onCollisionExit = function (e, t) {
    var o = e.node;

    if (o.group, o.isValid && 1 == t.tag && 0 == e.tag && this.getSoliderInfo(o)) {
      var a = this.target_list.indexOf(o);
      this.target_list.splice(a, 1);
    }
  }, t.prototype.getSoliderInfo = function (e) {
    var t = e.name;
    return t == "soldier_team_" + (this.team_info.team_info == s.TeamInfo.left_team ? 2 : 1) || "role_team" == t.substring(0, 9) && this.team_info.team_info % 2 == Number(i["default"].getNameType(t, "role_team_")) % 2 || "tower" == t.substring(0, 5) && this.team_info.team_info != e.getComponent(_["default"]).getTeamInfo();
  }, t.prototype.addRoleEffects = function (e) {
    var t = l["default"].getInstance().creatPlane(s.Pool_Type.role_effects);
    t.setParent(this.node), t.getComponent(c["default"]).showEffectInfo(e);
  }, oo([f(sp.Skeleton)], t.prototype, "soldier_skin", void 0), oo([m], t);
}(a["default"]);

o["default"] = v;

cc._RF.pop();